package gr.barbarosa.sshRemote.sdManipulation;

import android.os.Environment;
import android.util.Log;
import android.util.Xml;

import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import gr.barbarosa.sshRemote.constants.constants;

/**
 * Created by ilias.karditsis on 12/5/2014.
 */
public class filesManipulation {


    public static ArrayList<String> readCommands() {
        ArrayList<String> commandsList = new ArrayList<String>();
        File directory = new File(Environment.getExternalStorageDirectory() + File.separator + constants.mainAppFolderName + File.separator + constants.commandsAppFolder);
        for (File f : directory.listFiles()) {
            Log.v("test", f.toString());
        }
        return null;
    }


    public static boolean createFile(String folder, String Name, HashMap<String, String> parameters) {

        if (folder == constants.connectionsAppFolder) {
            ArrayList<String> files = findFolderFiles(constants.connectionsAppFolder);
            if (files.contains(Name)) {
                return false;
            } else {
                File newConnectionXmlFile = new File(folderManipulation.getConnectionsFolder() + File.separator + Name + ".xml");
                try {
                    writeXml(newConnectionXmlFile, folder, parameters);
                    return true;
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return false;
                }
                // FileOutputStream fileos= getApplicationContext().openFileOutput(xmlFile, Context.MODE_PRIVATE);


            }
        } else {
            return false;
        }


    }

    private static void writeXml(File newConnectionXmlFile, String MainTag, HashMap<String, String> parameters) throws FileNotFoundException {
        FileOutputStream fos = new FileOutputStream(newConnectionXmlFile);
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        XmlSerializer serializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        try {
            serializer.setOutput(writer);
            serializer.startDocument("UTF-8", true);
            serializer.startTag("", MainTag);

            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                serializer.startTag("", entry.getKey());
                serializer.attribute("", entry.getKey(), entry.getValue());
                serializer.endTag("", entry.getKey());
            }
            serializer.endTag("", MainTag);
            serializer.endDocument();
            osw.write(writer.toString());
            //return writer.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //Method get all files of folder
    private static ArrayList findFolderFiles(String FolderName) {
        ArrayList<String> files = new ArrayList<String>();
        File directory = new File(constants.mainAppFolderName + File.separator + FolderName);
        // get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList) {
            if (file.isFile()) {
                files.add(file.getName());
            }
        }
        return files;
    }


}
